﻿Public Class Form12

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Form2.BeginFile()
    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        Form2.EndFile(Form2.TextBox11.Text)
    End Sub

    Function Query(ByVal Name As String, ByVal Surname As String _
                   , ByVal Email1 As String, ByVal Email2 As String, ByVal Email3 As String _
                   , ByVal Namel As String, ByVal Surnamel As String, ByVal Emailp As String _
                   , ByVal CheckBox1 As Object, ByVal CheckBox2 As Object, ByVal CheckBox3 As Object) As Boolean

        Dim Ans As Boolean = True

        Dim Emailpr As String = "*" & Emailp & "*"

        Dim Nameq As Boolean
        Dim Surnameq As Boolean
        Dim Emailq As Boolean

        If CheckBox1.Checked Then
            If Ans <> False Then
                If Name Like Namel Then
                    Nameq = True
                Else
                    Nameq = False
                End If
                Ans = Nameq
            End If
        End If

        If CheckBox2.Checked Then
            If Ans <> False Then
                If Surname Like Surnamel Then
                    Surnameq = True
                Else
                    Surnameq = False
                End If
                Ans = Surnameq
            End If
        End If

        If CheckBox3.Checked Then
            If Ans <> False Then
                If Email1 Like Emailpr Or Email2 Like Emailpr Or Email3 Like Emailpr Then
                    Emailq = True
                Else
                    Emailq = False
                End If
                Ans = Emailq
            End If
        End If

        Return Ans
    End Function

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

        Try
            Form2.CheckBox3.CheckState = CheckState.Unchecked
            Form2.TextBox6.Text = 0
            Form2.TextBox11.Text = 0
            Dim ID As Integer = Form2.TextBox6.Text
            Dim Contacts As XElement = XElement.Load(Form1.TextBox7.Text & Form1.TextBox3.Text & ".xml")
            Dim Counter As XElement = _
            <Root>
                <%= From el In Contacts.<Counter>.<Count> _
                    Select el %>
            </Root>
            Dim Count As Integer = Counter

            Do Until ID = Count
                ID = ID + 1
                Form2.TextBox6.Text = ID

                Form2.LoadData()

                Dim Name As String = Form2.TextBox1.Text
                Dim Surname As String = Form2.TextBox2.Text
                Dim Email1 As String = Form2.TextBox3.Text
                Dim Email2 As String = Form2.TextBox4.Text
                Dim Email3 As String = Form2.TextBox5.Text

                Dim Namel As String = ComboBox1.SelectedItem
                Dim Surnamel As String = ComboBox2.SelectedItem
                Dim Emailp As String = TextBox1.Text

                If Query(Name, Surname, Email1, Email2, Email3, Namel, Surnamel, Emailp _
                         , CheckBox1, CheckBox2, CheckBox3) = True Then

                    Dim QID As Integer = Form2.TextBox11.Text
                    QID = QID + 1
                    Form2.TextBox11.Text = QID

                    Form2.WriteData(Form2.TextBox11.Text, Form2.TextBox6.Text)

                End If
            Loop
        Catch ex As Exception
            MsgBox("Query Failed!")
        End Try
    End Sub
End Class